package io.rehuo.modules.app.controller;

import com.alibaba.fastjson.JSONObject;
import io.rehuo.common.utils.R;
import io.rehuo.common.validator.ValidatorUtils;
import io.rehuo.modules.app.annotation.Login;
import io.rehuo.modules.app.annotation.LoginUser;
import io.rehuo.modules.app.entity.UserEntity;
import io.rehuo.modules.app.form.UserOneFormEntity;
import io.rehuo.modules.app.form.UserThreeFormEntity;
import io.rehuo.modules.app.form.UserTwoFormEntity;
import io.rehuo.modules.app.service.ApplyService;
import io.rehuo.modules.app.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author shiguang
 * @date 2022年09月10日 23:06
 */
@RestController
@RequestMapping("/app/apply")
@Api("贷款管理")
public class ApplyController {

    @Autowired
    private UserService userService;

    @Autowired
    private ApplyService applyService;

    /**
     * No such property: code for class: Script1
     *
     * @return : No such property: code for class: Script1
     * @author : shiguang
     * @description : (保存用户基础信息)
     * @date : 2022/9/10 23:08
     */
    @Login
    @PostMapping(value = "saveOne", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "保存用户基础信息1", notes = "保存用户基础信息1")
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 401, message = "权限错误"),
            @ApiResponse(code = 500, message = "系统错误"),
    })
    public R saveOne(@LoginUser UserEntity user, @RequestBody UserOneFormEntity entity) {
        //表单验证
        ValidatorUtils.validateEntity(entity);
        userService.saveUserOne(entity, user);
        return R.ok();
    }

    @Login
    @PostMapping(value = "saveTwo", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "保存用户基础信息2", notes = "保存用户基础信息2")
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 401, message = "权限错误"),
            @ApiResponse(code = 500, message = "系统错误"),
    })
    public R saveTwo(@LoginUser UserEntity user, @RequestBody UserTwoFormEntity entity) {
        //表单验证
        ValidatorUtils.validateEntity(entity);
        userService.saveUserTwo(entity, user);
        return R.ok();
    }

    @Login
    @PostMapping(value = "saveThree", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "保存用户基础信息3", notes = "保存用户基础信息3")
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 401, message = "权限错误"),
            @ApiResponse(code = 500, message = "系统错误"),
    })
    public R saveUserThree(@LoginUser UserEntity user, @RequestBody UserThreeFormEntity entity) {
        //表单验证
        ValidatorUtils.validateEntity(entity);
        userService.saveUserThree(entity, user);
        return R.ok();
    }

    @Login
    @PostMapping(value = "saveApply", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "申请贷款", notes = "申请贷款")
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 401, message = "权限错误"),
            @ApiResponse(code = 500, message = "系统错误"),
    })
    public R saveApply(@LoginUser UserEntity user) {
        applyService.saveApply(user);
        return R.ok();
    }

    @Login
    @GetMapping(value = "getList")
    @ApiOperation(value = "获取贷款列表", notes = "获取贷款列表")
    @ApiImplicitParam(name = "type", value = "类型,1:审核结果  2：我的贷款  3：还款记录", paramType = "query", dataTypeClass = String.class)
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 401, message = "权限错误"),
            @ApiResponse(code = 500, message = "系统错误"),
    })
    public R getList(@LoginUser UserEntity userEntity, @RequestParam String type) {
        return R.ok().put("list", applyService.getListByType(type, userEntity));
    }

    @Login
    @GetMapping(value = "getInfo")
    @ApiOperation(value = "获取贷款详情", notes = "获取贷款详情")
    @ApiImplicitParam(name = "applyId", value = "贷款ID", paramType = "query", required = true, dataTypeClass = Long.class)
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 401, message = "权限错误"),
            @ApiResponse(code = 500, message = "系统错误"),
    })
    public R getInfo(@RequestParam Long applyId) {
        return R.ok().put("data", applyService.selectById(applyId));
    }

    @Login
    @PostMapping(value = "userWithdrawal")
    @ApiOperation(value = "申请提现", notes = "申请提现")
    @ApiImplicitParam(name = "applyId", value = "贷款ID", paramType = "query", required = true, dataTypeClass = Long.class)
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 401, message = "权限错误"),
            @ApiResponse(code = 500, message = "系统错误"),
    })
    public R userWithdrawal(@LoginUser UserEntity userEntity, @RequestBody JSONObject object) {
        applyService.userWithdrawal(userEntity, object.getLong("applyId"));
        return R.ok();
    }


    @Login
    @GetMapping(value = "getPaymentBankInfo")
    @ApiOperation(value = "获取还款银行卡信息", notes = "获取还款银行卡信息")
    @ApiImplicitParam(name = "applyId", value = "贷款ID", paramType = "query", required = true, dataTypeClass = Long.class)
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 401, message = "权限错误"),
            @ApiResponse(code = 500, message = "系统错误"),
    })
    public R getPaymentBankInfo(@RequestParam Long applyId,@LoginUser UserEntity userEntity) {
        return R.ok(applyService.getPaymentBankInfo(userEntity, applyId));
    }
}
